﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ValueType.Core;
using System.Collections;

namespace ValueType
{
    class Program
    {
        static void Main(string[] args)
        {
            //TestStruct();
            //TestValueMethod();
            //TestValue2Method();
            //集合初始化器
            //TestArrayList();

            //TestHashTable();
            //TestListT();
            Dictionary<string, Girl> table = new Dictionary<string, Girl>();


            Girl a = new Girl() { Name = "黄芩" };
            Girl b = new Girl() { Name = "张莉" };
            Boy c = new Boy() { Name = "黎明" };
            table.Add("a", a);
            table.Add("b", b);

            foreach (Girl item in table.Values)
            {
                Girl g = (Girl)item;
                Console.WriteLine("女孩:{0}", item.Name);
            }
            





            Console.Read();
        }

        private static void TestListT()
        {
            Girl a = new Girl() { Name = "黄芩" };
            Girl b = new Girl() { Name = "张莉" };
            Boy c = new Boy() { Name = "黎明" };

            List<Girl> girlRoom = new List<Girl>();
            girlRoom.Add(a);
            girlRoom.Add(b);

            foreach (Girl item in girlRoom)
            {
                Console.WriteLine("女孩:{0}", item.Name);
            }
        }

        private static void TestHashTable()
        {
            Student john = new Student() { Name = "John" };
            Student joe = new Student() { Name = "Joe" };
            Student hehuxiao = new Student() { Name = "和呼啸" };
            Student yzc = new Student() { Name = "叶总裁" };

            Hashtable table = new Hashtable();
            table.Add("a", joe);
            table.Add("b", john);
            table.Add("c", hehuxiao);
            table.Add("d", yzc);
            Console.WriteLine("table元素的个数:{0}", table.Count);
            //遍历所有的值,存储的全是object
            foreach (object item in table.Values)
            {
                Student stu = (Student)item;//注意转换类型
                Console.WriteLine(stu.Name);
            }

            //遍历所有的key，存储的全是object
            foreach (object key in table.Keys)
            {
                Console.WriteLine(key);
            }

            //遍历键值对(key-value),此时类型是DictionaryEntry
            foreach (DictionaryEntry item in table)
            {
                Console.WriteLine("key:{0}", item.Key);
                Student stuValue = (Student)item.Value;//注意转换类型
                Console.WriteLine("value:{0}", stuValue.Name);
            }

        }

        private static void TestArrayList()
        {
            Student john = new Student() { Name = "John" };
            Student joe = new Student() { Name = "Joe" };
            Student hehuxiao = new Student() { Name = "和呼啸" };
            ArrayList list = new ArrayList();
            list.Add(joe);
            list.Add(john);
            list.Add(hehuxiao);
            PrintStudent(list);

            list.RemoveAt(0);
            PrintStudent(list);

            list.Remove(hehuxiao);
            PrintStudent(list);
        }

        private static void PrintStudent(ArrayList list)
        {
            Console.WriteLine("-----------");
            for (int i = 0; i < list.Count; i++)
            {
                Student stu = (Student)list[i];
                Console.WriteLine("{0}.{1}", i, stu.Name);
            }
        }

        private static void TestValue2Method()
        {
            int a = 10;
            Console.WriteLine("a:{0}", a);//=>10
            ChangeValue(a);//给的是值
            Console.WriteLine("a:{0}", a);//=>10
            ChangeValue2(ref a);//使用ref将值类型改成引用类型
            Console.WriteLine("a:{0}", a);//=>20


            Console.WriteLine("-----------");

            Student b = new Student(10);
            Console.WriteLine("b:{0}", b.Age);//=>10
            ChangeValue(b);//给的是地址
            Console.WriteLine("b:{0}", b.Age);//=>20
        }

        public static void ChangeValue(int a)
        {
            a = 20;
        }
        public static void ChangeValue2(ref int a)
        {
            a = 20;
        }

        public static void ChangeValue(Student stu)
        {
            stu.Age = 20;
        }

        private static void TestValueMethod()
        {
            int a = 10;
            object b = a;//a=>值类型 b=》引用类型  a-->b=>装箱
            a = 20;
            Console.WriteLine("a:{0},b:{1}", a, b);
            Console.WriteLine("----------------");
            object x = 10;
            int y = (int)x;//x=>引用类型 y=>值类型 x-->y=>拆箱
            x = 20;
            Console.WriteLine("x:{0},y:{1}", x, y);
        }

        /// <summary>
        /// 值类型 =-- 结构
        /// </summary>
        private static void TestStruct()
        {
            int aa = 10, bb = aa;
            Console.WriteLine("aa={0},bb={1}", aa, bb);
            aa = 20;
            Console.WriteLine("aa={0},bb={1}", aa, bb);
            Console.WriteLine("------------分割线-------------");
            //类和数组:引用类型
            Student xiaoming = new Student(10);
            Student xiaohong = xiaoming;//两者相同
            Console.WriteLine("xiaoming:{0},xiaohong:{1}", xiaoming.Age, xiaohong.Age);
            xiaohong.Age = 23;
            Console.WriteLine("xiaoming:{0},xiaohong:{1}", xiaoming.Age, xiaohong.Age);

            Console.WriteLine("------------分割线-------------");
            //结构是值类型
            LittleBoy dabai = new LittleBoy();
            dabai.Age = 10;
            LittleBoy xiaobai = dabai;
            Console.WriteLine("xiaobai:{0},dabai:{1}", xiaobai.Age, dabai.Age);
            xiaobai.Age = 5;
            Console.WriteLine("xiaobai:{0},dabai:{1}", xiaobai.Age, dabai.Age);
            Console.WriteLine("------------分割线-------------");

            byte a = 1;//struct
            short b = 2;//struct
            int c = 3;//struct
            long d = 4;//struct

            float e = 5.0f;//struct
            double f = 6.0;//struct

            char g = '7';//struct
            bool h = true;//struct
            string i = "8";//class
            string ii = new String(new char[] { '8' });

        }
    }
}
